<?php
include_once('config/conn.php');

$LoginAdmin = auth();

if (!empty($_GET['logout'])) {
    if ($_GET['logout'] == 1) {
        session_unset();
        ShowMsg('退出成功', 'login.php');
    }
}

$collegeList = "SELECT * FROM college";

$college = find($collegeList);

if (!empty($_POST['name'])) {
    $name = $_POST['name'];
    $sql = "SELECT name FROM `college` WHERE `name` = '$name'";

    $findData = find($sql);
    if ($findData) {
        ShowMsg('学院已存在，请重新输入');
    }

    $data = [
        'name' => $name,
        'create_time' => date('Y-m-d H:i:s', time()),
        'update_time' => date('Y-m-d H:i:s', time())
    ];
    $res = add('college', $data);
    if ($res) {
        ShowMsg('新增学院成功', 'index.php');
    } else {
        ShowMsg('新增学院失败');
    }
}

if (!empty($_POST['collegeName'])) {
    $id = $_POST['ID'];
    $data = [
        'name' => $_POST['collegeName'],
        'update_time' => date('Y-m-d H:i:s', time())
    ];
    $result = update('college', $data, "`id` = '$id'");
    if ($result) {
        ShowMsg('更新成功', 'index.php');
    } else {
        ShowMsg('更新失败', 'index.php');
    }
}

if (!empty($_POST['deleteName'])) {
    $name = $_POST['deleteName'];
    $Sql = "SELECT * FROM `college` WHERE `name` = '$name'";
    $admin = find($Sql);

    if (!$admin) {
        error('删除的学院不存在');
    }
    $result = delete('college', "`name` = '$name'");

    if ($result) {
        ShowMsg('删除该学院成功');
    } else {
        ShowMsg('删除该学院失败');
    }
}

?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学生管理系统首页</title>
    <?php include_once('basic/meta.php') ?>
    <style>
        th,
        td {
            height: 30px;
            line-height: 30px;
            font-family: 华文中宋;
            font-weight: 400;
        }

        th {
            font-weight: 600;
        }
    </style>
</head>

<body>
    <?php include_once('basic/header.php'); ?>
    <?php include_once('basic/nav.php'); ?>
    <div class="main">
        <div class="card">
            <div class="card-body" style="font-weight: 600;">
                <div style="display: flex; justify-content: space-between">
                    <p>学院信息管理</p>
                    <button style="height: 40px; font-family: 华文中宋;"
                        type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#collegeAdd">
                        添加学院
                    </button>
                    <!-- 模态框 -->
                    <div class="modal fade" id="collegeAdd" tabindex="-1" aria-labelledby="myModalLabel" aria-hidden="true" style="position: absolute; z-index: 9999;">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <form action="" method="post">
                                    <!-- 模态框头部 -->
                                    <div class="modal-header">
                                        <h5 class="modal-title" id="myModalLabel" style="font-family: 华文中宋;">创建学院</h5>
                                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                    </div>

                                    <!-- 模态框主体 -->
                                    <div class="modal-body">
                                        <input
                                            class="input is-primary"
                                            type="text"
                                            name="name"
                                            placeholder=" College Name input" />
                                    </div>
                                    <!-- 模态框底部 -->
                                    <div class="modal-footer" style="font-family: 华文中宋;">
                                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                                        <button type="submit" class="btn btn-secondary" data-bs-dismiss="modal">提交</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="overflow-auto" style="height: 515px; margin-top:20px">
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <th scope="col">ID</th>
                                <th scope="col">学院名称</th>
                                <th scope="col">创建时间</th>
                                <th scope="col">更新时间</th>
                                <th scope="col">操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($college as $key => $item) {
                            ?>
                                <tr>
                                    <?php foreach ($item as $k => $v) {  ?>
                                        <td><?php echo $v; ?></td>
                                    <?php } ?>
                                    <td>
                                        <button type="button" class="btn btn-primary" style="height: 35px; " data-bs-toggle="modal" data-bs-target="#collegeEdit" data-table-id="<?php echo $item[0]; ?>">
                                            <i class="iconfont icon-doc-edit"></i>
                                        </button>
                                        <button type="button" class="btn btn-primary" style="height: 35px;" data-bs-toggle="modal" data-bs-target="#collegeDelete" data-table-name="<?php echo $item[1]; ?>">
                                            <i class="iconfont icon-delete"></i>
                                        </button>
                                    </td>
                                </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                    <!-- 模态框 -->
                    <div class="modal fade" id="collegeEdit" tabindex="-1" aria-labelledby="myModalLabel" aria-hidden="true" style="position: absolute; z-index: 9999;">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <form action="" method="post">
                                    <!-- 模态框头部 -->
                                    <div class="modal-header">
                                        <h5 class="modal-title" id="myModalLabel" style="font-family: 华文中宋;">修改学院信息</h5>
                                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                    </div>

                                    <!-- 模态框主体 -->
                                    <div class="modal-body">
                                        <input type="text" name="ID" value="" class="input is-primary" id="ID" style="height: 40px;" disabled />
                                        <input
                                            class="input is-primary"
                                            type="text"
                                            name="collegeName"
                                            style="height: 40px; margin-top: 20px"
                                            placeholder=" College Name input" />
                                    </div>
                                    <!-- 模态框底部 -->
                                    <div class="modal-footer" style="font-family: 华文中宋;">
                                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                                        <button type="submit" class="btn btn-secondary" data-bs-dismiss="modal" onclick="updateCollege()">提交</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                    <div class="modal fade" id="collegeDelete" tabindex="-1" aria-labelledby="myModalLabel" aria-hidden="true" style="position: absolute; z-index: 9999;">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <form action="" method="post">
                                    <!-- 模态框头部 -->
                                    <div class="modal-header">
                                        <h5 class="modal-title" id="myModalLabel" style="font-family: 华文中宋;">是否确认删除学院信息</h5>
                                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                    </div>
                                    <!-- 模态框主体 -->
                                    <div class="modal-body">
                                        <input type="text" name="deleteName" value="" class="input is-primary" id="deleteName" style="height: 40px;" disabled />
                                    </div>
                                    <!-- 模态框底部 -->
                                    <div class="modal-footer" style="font-family: 华文中宋;">
                                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                                        <button type="submit" class="btn btn-secondary" data-bs-dismiss="modal" onclick="deleteCollege()">提交</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>


    </div>
    <script>
        // Get the modal
        var modal = document.getElementById('collegeEdit');
        var deleteModal = document.getElementById('collegeDelete');
        var btns = document.querySelectorAll('button[data-bs-target="#collegeEdit"]');

        btns.forEach(function(btn) {
            btn.addEventListener('click', function() {
                var tableId = this.getAttribute('data-table-id');
                document.getElementById('ID').value = tableId;

            });
        });

        var btnDelete = document.querySelectorAll('button[data-bs-target="#collegeDelete"]');
        btnDelete.forEach(function(btn) {
            btn.addEventListener('click', function() {
                var deleteName = this.getAttribute('data-table-name');
                document.querySelector('input[name="deleteName"]').value = deleteName;
            });
        });

        // 更新数据事件
        function updateCollege() {
            var collegeName = document.querySelector('input[name="collegeName"]').value;
            document.querySelector('#ID').disabled = false;
            if (collegeName == '') {
                alert('请输入学院名称')
            }
        }

        // 删除数据事件
        function deleteCollege() {
            document.querySelector('#deleteName').disabled = false;
        }
    </script>
</body>

</html>